<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 11:20
 */

namespace HiCommon\Repository;

class BaseRepository
{
    public function model_child(){
        return get_called_class()::base_model();
    }




    /**
     * 批量更新数据
     *
     * @param array $arr_data
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_update_batch($arr_data = [])
    {
        return self::model_child()->UpdateBatch($arr_data);
    }

    /**
     * 获取全部数据-option
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_option($arr_option = [])
    {
        return self::model_child()->getoption($arr_option);
    }

    /**
     * 获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_all($arr_option = [])
    {
        return self::base_get_option($arr_option)->get();
    }

    /**
     * 获取全部数据
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_all_by_where($arr_where = [])
    {
        return self::model_child()->where($arr_where)->get();
    }

    /**
     * 获取全部数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_all_orwhere($arr_where = [], $arr_option = [])
    {
        return self::base_get_option($arr_option)->orwhere($arr_where)->get();
    }

    /**
     * 获取全部数据
     *
     * @param string $s_field
     * @param array  $arr_ids
     * @param array  $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_all_by_wherein($s_field = 'id',$arr_ids = [],$arr_option = [])
    {
        return self::base_get_option($arr_option)->whereIn($s_field,$arr_ids)->get();
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_list($arr_option = [])
    {
        return self::model_child()->commonpage($arr_option);
    }

    /**
     * api接口分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_api_get_list($arr_option = [])
    {
        return self::model_child()->apipage($arr_option);
    }



    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_add_one($arr_input)
    {
        $arr_field_save = self::base_table_keys();
        $arr_input = array_remain($arr_input,$arr_field_save);//过滤掉非表中的字段
        return self::model_child()->create($arr_input);
    }

    /**
     * 添加多条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_add_many($arr_input)
    {
        return self::model_child()->insert($arr_input);
    }

    /**
     * 查询一条数据-option
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_find_option($arr_option = [])
    {
        return self::model_child()->findoption($arr_option);
    }


    /**
     * 根据主键id查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_find_one(int $n_id, $arr_option = [])
    {
        return self::base_find_option($arr_option)->find($n_id);
    }

    /**
     * 根据条件查询一条数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_one($arr_where = [], $arr_option = [])
    {
        return self::base_find_option($arr_option)->where($arr_where)->first();
    }

    /**
     * 根据条件查询一条数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_one_orwhere($arr_where = [], $arr_option = [])
    {
        return self::base_find_option($arr_option)->orwhere($arr_where)->first();
    }

    /**
     * 根据主键id删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_delete_one(int $n_id,$bool = false)
    {
        return self::model_child()->where('id', $n_id)
                           ->commondelete($bool);
    }

    /**
     * 根据where条件删除数据
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_delete_by_where($arr_where = [],$bool = false)
    {
        return self::model_child()->where($arr_where)
                   ->commondelete($bool);
    }

    /**
     * 根据wherein条件删除数据
     *
     * @param string $s_field
     * @param array  $arr_wherein
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_delete_by_wherein($s_field = 'id',$arr_wherein = [],$bool = false)
    {
        return self::model_child()->whereIn($s_field,$arr_wherein)
                   ->commondelete($bool);
    }

    /**
     * 根据主键id修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_edit_one(array $arr_input)
    {

        $arr_field_save = self::base_table_keys();
        $arr_input = array_remain($arr_input,$arr_field_save);//过滤掉非表中的字段
        return self::model_child()->where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 根据where条件修改1条以上的数据
     *
     * @param array $arr_where
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_edit_by_where($arr_where = [] , $arr_input = [])
    {
        return self::model_child()->where($arr_where)->update($arr_input);
    }

    /**
     * 根据条件创建或者更新数据
     *
     * @param array $arr_where
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_update_or_create($arr_where = [] , $arr_input = [])
    {
        $arr_field_save = self::base_table_keys();
        $arr_input = array_remain($arr_input,$arr_field_save);//过滤掉非表中的字段
        return self::model_child()->updateOrCreate($arr_where,$arr_input);
    }

    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_count_by_where($arr_where)
    {
        return self::model_child()->where($arr_where)->count();
    }

    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return self::model_child()->where($arr_where)->sum($s_field);
    }

    /**
     * 获取表字段集合
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_table_keys(){
        return self::model_child()->table_field_keys();
    }

    /**
     * 根据wherein条件更新数据
     *
     * @param string $s_field
     * @param array  $arr_wherein
     * @param array  $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function base_edit_by_wherein($arr_wherein = [],$arr_input = [],$s_field = 'id')
    {
        return self::model_child()->whereIn($s_field,$arr_wherein)
                   ->update($arr_input);
    }

}